﻿using BPMN.Application.IService;
using BPMN.Domain;
using BPMN.Domain.Entity.User;
using BPMN.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using BPMN.DomianShare;
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using BPMN.Application.Contracts.DTO.Request.UserInfo;
using BPMN.Application.Contracts.DTO.Request.Role;
using BPMN.Application.Contracts.DTO.Response.Role;
using BPMN.Application.Contracts.DTO.Response;
using BPMN.Application.Contracts.DTO.Request.PerRole;
using BPMN.Application.Contracts.DTO.Response.RolePermission;
using BPMN.Domain.Entity.Custom;
using BPMN.Application.Contracts.DTO.Request.Custom;
using BPMN.Application.Contracts.DTO.Response.Custom;

namespace BPMN.Application.Service
{
    public class CustomAppService : AppServiceBase<Custom>, ICustomAppService
    {
        private readonly IRepositoryBase<Custom> RepCustom;
        private readonly IMapper mapper;

        public  CustomAppService(IRepositoryBase<Custom> RepCustom, IMapper mapper):base(RepCustom)
        {
            this.RepCustom = RepCustom;
            this.mapper = mapper;
        }

        public async Task<ResultResponseDto<int>> AddCustomInfo(CustomRequstDto entity)
        {
            var list=await RepCustom.GetAll().Result.Where(x=>x.FormCode== entity.FormCode).FirstAsync();
            var n = 0;
            if (list != null)
            {
                return new ResultResponseDto<int>()
                {
                    Code = ResultCode.Fail,
                    ErrorInfo = "已经存在",
                };
            }
            else
            {
                var obj=mapper.Map<Custom>(entity);
                n=await RepCustom.Add(obj);
            }
            if (n > 0)
            {
                return new ResultResponseDto<int>();
            }
            else
            {
                return new ResultResponseDto<int>(ResultCode.Fail, "添加失败");
            }
        }

        public Task<ResultResponseDto<int>> DeleleCustomInfo(int Customid)
        {
            throw new NotImplementedException();
        }

        public Task<ResultResponseDto<List<CustomShowResponseDto>>> GetCustomInfo()
        {
            throw new NotImplementedException();
        }

        public Task<ResultResponseDto<CustomShowResponseDto>> GetCustomInfoById(int Id)
        {
            throw new NotImplementedException();
        }

        public Task<ResultResponseDto<int>> UpdateCustomInfo(CustomRequstDto entity)
        {
            throw new NotImplementedException();
        }



        ///// <summary>
        ///// 添加角色权限表
        ///// </summary>
        ///// <param name="entity"></param>
        ///// <returns></returns>
        //public async Task<ResultResponseDto<int>> AddRolePer(PermissionRoleRequstDto entity)
        //{
        //    var slist = RepRolePermission.GetAll().Result.Where(x => x.RoleId == entity.RoleId).ToList();
        //    if (slist.Count() != 0)
        //    {
        //        await RepRolePermission.RealDel(slist);
        //    }

        //    List<RolePermission> rolePermission = new List<RolePermission>();
        //    foreach (var item in entity.PermissionId.Split(','))
        //    {
        //        rolePermission.Add(new RolePermission { RoleId = entity.RoleId, PermissionId = int.Parse(item) });
        //    }
        //    int n = await RepRolePermission.AddRange(rolePermission);
        //    if (n > 0)
        //    {
        //        return new ResultResponseDto<int>();
        //    }
        //    else
        //    {
        //        return new ResultResponseDto<int>(ResultCode.Fail, "添加失败");
        //    }

        //}

        //public Task<ResultResponseDto<int>> UpdateCustomInfo(CustomRequstDto entity)
        //{
        //    throw new NotImplementedException();
        //}
        ///// <summary>
        ///// 角色显示功能
        ///// </summary>
        ///// <returns></returns>
        //public async Task<ResultResponseDto<List<RoleShowResponseDto>>> GetRole()
        //{
        //    var slist = await (await RepRole.GetAll()).Where(x => x.IsDel == true).ToListAsync();

        //    return new ResultResponseDto<List<RoleShowResponseDto>>()
        //    {
        //        Code = ResultCode.Ok,
        //        ErrorInfo = "获取成功",
        //        Data = mapper.Map<List<RoleShowResponseDto>>(slist)
        //    };
        //}
        ///// <summary>
        ///// 根据Id获取角色信息
        ///// </summary>
        ///// <param name="Id"></param>
        ///// <returns></returns>
        //public async Task<ResultResponseDto<RoleShowResponseDto>> GetRoleById(int Id)
        //{
        //    var slist = await RepRole.Get(Id);

        //    return new ResultResponseDto<RoleShowResponseDto>()
        //    {
        //        Code = ResultCode.Ok,
        //        ErrorInfo = "获取成功",
        //        Data = mapper.Map<RoleShowResponseDto>(slist)
        //    };

        //}
        ///// <summary>
        ///// 反填权限
        ///// </summary>
        ///// <param name="RoleId"></param>
        ///// <returns></returns>
        //public async Task<ResultResponseDto<List<RolePermissionResponseShowDto>>> GetRolePermissionByRoleId(int RoleId)
        //{
        //    return new ResultResponseDto<List<RolePermissionResponseShowDto>>()
        //    {
        //        Code = ResultCode.Ok,
        //        ErrorInfo = "获取成功",
        //        Data = mapper.Map<List<RolePermissionResponseShowDto>>(RepRolePermission.GetAll().Result.Where(x => x.RoleId == RoleId).ToList())
        //    };
        //}
        ///// <summary>
        ///// 修改功能
        ///// </summary>
        ///// <param name="entity"></param>
        ///// <returns></returns>
        //public async Task<ResultResponseDto<int>> UpdateRole(RoleRequstDto entity)
        //{
        //    var slist = await (await RepRole.GetAll()).FirstOrDefaultAsync();

        //    var obj = mapper.Map<Role>(entity);
        //    int n = await RepRole.Update(obj);
        //    if (n > 0)
        //    {
        //        return new ResultResponseDto<int>();
        //    }
        //    else
        //    {
        //        return new ResultResponseDto<int>(ResultCode.Fail, "添加失败");
        //    }
        //}
    }
}

